package cst.a720yyq.zyfypt_720yyq;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cst.a720yyq.zyfypt_720yyq.Bean.LoginBean;
import cst.a720yyq.zyfypt_720yyq.Interface.ResService;
import cst.a720yyq.zyfypt_720yyq.Interface.ToudouListener;
import cst.a720yyq.zyfypt_720yyq.Modle.ResModle;
import cst.a720yyq.zyfypt_720yyq.View.toasty.Toasty;
import retrofit2.Retrofit;

public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button btn_login;
//    private Button btn_reg;
    private ResModle modle;
    private TextView btn_TVW;
    private ToudouListener<LoginBean>listener;
    private Intent intent1,intent2;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = (EditText)findViewById(R.id.login_username);
        password = (EditText)findViewById(R.id.login_password);
        btn_login=(Button)findViewById(R.id.btn_login);
//        btn_reg = (Button)findViewById(R.id.btn_reg);
        btn_TVW =(TextView)findViewById(R.id.btn_TVW);
        intent1 =new Intent(LoginActivity.this,MainActivity.class);

        sp = getSharedPreferences("ME",MODE_PRIVATE);
        editor = sp.edit();

        listener = new ToudouListener<LoginBean>() {
            @Override
            public void onResponce(LoginBean l) {
                if(l.getUsername()!=null){
                    String sessionid = l.getSessionid();//把SID存到字符串内方便服务器验证;
                    editor.putString("username",l.getUsername());
                    editor.putString("password",password.getText().toString());
                    editor.putString("session_id",sessionid);
                    editor.commit();

                    startActivity(intent1);
                    finish();
                }
                else {
                    Toasty.warning(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toasty.error(LoginActivity.this,"网络连接错误，请检查网络是否连通",Toast.LENGTH_SHORT).show();
            }
        };
        intent2 = new Intent(LoginActivity.this, RegActivity.class);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.length()!=0)
                {
                    modle = new ResModle("gson","");
                    modle.login(username.getText().toString(), password.getText().toString(), listener);
                }
                else Toasty.warning(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
            }
        });
//        btn_reg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LoginActivity.this,RegActivity.class);
//                startActivity(intent);
//                finish();
//            }
//        });
        btn_TVW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent2);
                finish();
            }
        });

    }
}
